package com.pet.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pet.context.BaseContext;
import com.pet.dto.web.InvitationInformationDTO;
import com.pet.dto.web.InvitationRemarkDTO;
import com.pet.entity.InvitationCollect;
import com.pet.entity.InvitationInformation;
import com.pet.entity.InvitationLike;
import com.pet.entity.InvitationRemark;
import com.pet.exchange.PetInvitationInfoUtil;
import com.pet.mapper.InvitationMapper;
import com.pet.mapper.UserInformationMapper;
import com.pet.pageDto.InvitationPageDTO;
import com.pet.result.PageResult;
import com.pet.service.InvitationService;
import com.pet.vo.web.InvitationCollectVO;
import com.pet.vo.web.InvitationHotVO;
import com.pet.vo.web.InvitationInformationVO;
import com.pet.vo.web.InvitationRemarkVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class InvitationServiceImpl implements InvitationService {

    @Autowired
    private InvitationMapper invitationMapper;
    @Autowired
    private UserInformationMapper userInformationMapper;
    @Autowired
    private PetInvitationInfoUtil invitationInfoUtil;

    /**
     * 创建帖子
     * @param dto
     */
    public void insertInvitationInformation(InvitationInformationDTO dto) {
        //创建实体
        InvitationInformation entity = new InvitationInformation();
        //赋值
        BeanUtils.copyProperties(dto, entity);

        entity.setCreateTime(LocalDateTime.now());
        entity.setCreateUser(Math.toIntExact(BaseContext.getCurrentId()));
        entity.setUpdateTime(LocalDateTime.now());
        entity.setUpdateUser(Math.toIntExact(BaseContext.getCurrentId()));
        //操作数据库
        log.info("{}",entity);
        invitationMapper.insertInvitationInformation(entity);
    }

    /**
     * 查询帖子
     * @param dto
     * @return
     */
    public PageResult pageQuery(InvitationPageDTO dto) {
        Page<InvitationInformationVO> page = new Page<>();

        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        if(dto.getUserId() == null){
            dto.setUserId(null);
        }
        //使用dishPageQueryDTO作为参数，执行一个分页查询，并将查询结果（一个Page对象，其中包含了DishVO类型的元素）存储在page变量中。
        try (Page<InvitationInformation> entity = invitationMapper.pageQuery(dto)) {
            //log.info("entity:{}",entity.getResult());
            for (InvitationInformation e : entity) {
                InvitationInformationVO vo = new InvitationInformationVO();
                BeanUtils.copyProperties(e, vo);
                //log.info("{}",e);
                try {
                    if (e.getInvitationType() == 0) {
                        vo.setInvitationType("公益活动");
                    } else if (e.getInvitationType() == 1) {
                        vo.setInvitationType("科学养宠");
                    } else if (e.getInvitationType() == 2) {
                        vo.setInvitationType("发声墙");
                    } else if (e.getInvitationType() == 3) {
                        vo.setInvitationType("日常");
                    } else if (e.getInvitationType() == 4) {
                        vo.setInvitationType("其他");
                    }

                } catch (Exception exception) {
                    log.error(exception.getMessage());
                }
                vo.setCreateUserId(e.getCreateUser());
                vo.setCreateUser(userInformationMapper.selectName(e.getCreateUser()));
                //设置发布者头像
                vo.setProfilePhoto(userInformationMapper.selectProfilePhotoById(e.getCreateUser()));
                page.add(vo);
            }
        }
        log.info("{}",page);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 修改帖子
     * @param dto
     */
    public void updateInvitation(InvitationInformationDTO dto) {

        InvitationInformation invitationInformation = new InvitationInformation();

        BeanUtils.copyProperties(dto,invitationInformation);
        invitationInformation.setUpdateTime(LocalDateTime.now());
        log.info("invitationInformation:{}",invitationInformation);
        invitationMapper.updateInvitation(invitationInformation);
    }

    /**
     * 删除帖子
     * @param ids
     */
    @Transactional
    public void deleteInvitation(List<Integer> ids) {
        try {
            //根据帖子id删除相关的评论、点赞、收藏
            invitationMapper.deleteInvitationRemarksByInvitationId(ids);
            invitationMapper.deleteInvitationLikeByIds(ids);
            invitationMapper.deleteInvitationCollectByIds(ids);
            //删除帖子信息表
            invitationMapper.deleteInvitationByIds(ids);
        }catch (Exception exception){
            log.error(exception.getMessage());
            throw new RuntimeException("删除帖子失败");
        }
    }

    /**
     * 为帖子点赞
     * @param id
     */
    @Transactional
    public void invitationLike(Integer id) {
        //操作帖子信息表 修改帖子点赞数
        InvitationInformation e = invitationMapper.selectInvitationInfoById(id);
        Integer numberLike = e.getLikeNumber();

        invitationMapper.updateInvitationLikeNumber(numberLike+1, id);
        //操作帖子喜欢列表 添加点赞的用户的信息
        invitationMapper.insertInvitationLikeInfo(BaseContext.getCurrentId(),id);
    }

    /**
     * 查询帖子详情
     * @param id
     * @return
     */
    public InvitationInformationVO selectInvitationInfoById(Integer id) {
        InvitationInformation e = invitationMapper.selectInvitationInfoById(id);
        InvitationInformationVO vo = new InvitationInformationVO();
        try{
            BeanUtils.copyProperties(e,vo);
            try {
                if(e.getInvitationType() == 0){
                    vo.setInvitationType("公益活动");
                } else if (e.getInvitationType() == 1) {
                    vo.setInvitationType("科学养宠");
                }else if (e.getInvitationType() == 2) {
                    vo.setInvitationType("发声墙");
                }else if (e.getInvitationType() == 3) {
                    vo.setInvitationType("日常");
                }else if (e.getInvitationType() == 4) {
                    vo.setInvitationType("其他");
                }


            }catch (Exception exception){
                log.error(exception.getMessage());
            }
            vo.setCreateUserId(e.getCreateUser());
            vo.setCreateUser(userInformationMapper.selectName(e.getCreateUser()));
            //设置发布者头像
            vo.setProfilePhoto(userInformationMapper.selectProfilePhotoById(e.getCreateUser()));
        }catch (Exception exception){
            log.error(exception.getMessage());
            return null;
        }

        return vo;
    }

    /**
     * 取消帖子的点赞
     * @param id
     */
    public void deleteInvitationLike(Integer id) {
        //操作帖子信息表 修改帖子点赞数
        InvitationInformation e = invitationMapper.selectInvitationInfoById(id);
        Integer numberLike = e.getLikeNumber();

        if (numberLike != 0){
            invitationMapper.updateInvitationLikeNumber(numberLike-1, id);
        }

        //操作帖子喜欢列表 删除点赞的用户的信息
        invitationMapper.deleteInvitationLike(BaseContext.getCurrentId(),id);
    }

    /**
     * 收藏帖子
     * @param id
     */
    public void invitationCollect(Integer id) {
        //对帖子收藏数进行修改
        InvitationInformation e = invitationMapper.selectInvitationInfoById(id);
        Integer numberCollect = e.getCollectNumber();

        invitationMapper.updateInvitationCollectNumber(numberCollect+1, id);
        //操作帖子喜欢列表 添加点赞的用户的信息
        invitationMapper.insertInvitationCollect(BaseContext.getCurrentId(),id);
    }

    /**
     * 取消收藏帖子
     * @param invitationId
     */
    @Transactional
    public void deleteInvitationCollect(Integer invitationId) {
        //修改帖子信息表 修改帖子收藏数
        InvitationInformation e = invitationMapper.selectInvitationInfoById(invitationId);
        if(e == null)return;
        log.info("{}",e);
        Integer numberCollect = e.getCollectNumber();
        invitationMapper.updateInvitationCollectNumber(numberCollect-1,invitationId);

        //获取收藏表数据
        InvitationCollect invitationCollect =
                invitationMapper.selectInvitationCollectsById(invitationId,BaseContext.getCurrentId());
        //删除收藏列表的数据
        invitationMapper.deleteInvitationCollect(BaseContext.getCurrentId(),invitationCollect.getId());
    }

    /**
     * 查询个人收藏
     * @return
     */
    public List<InvitationCollectVO> selectInvitationCollectById() {
        //查询收藏表
        List<InvitationCollect> invitationCollects =
                invitationMapper.selectInvitationCollectsByUserId(BaseContext.getCurrentId());
        //查询帖子信息
        List<InvitationCollectVO> vos = new ArrayList<>();
        for (InvitationCollect invitationCollect : invitationCollects) {
            //收藏列表VO
            InvitationCollectVO vo = new InvitationCollectVO();
            //帖子详细详细
            InvitationInformation invitationInformation =
                    invitationMapper.selectInvitationInfoById(invitationCollect.getInvitationId());
            BeanUtils.copyProperties(invitationInformation,vo);
            vo.setInvitationId(invitationInformation.getId());
            vo.setId(invitationCollect.getId());
            //判断
            vo.setInvitationType(invitationInfoUtil.typeToStr(invitationInformation.getInvitationType()));
            vo.setCreateUser(userInformationMapper.selectName(invitationInformation.getCreateUser()));
            //设置创建者id
            vo.setCreateUserId(invitationInformation.getCreateUser());
            //设置创建者头像
            vo.setProfilePhoto(userInformationMapper.selectProfilePhotoById(invitationInformation.getCreateUser()));

            vos.add(vo);
        }

        return vos;
    }

    /**
     * 评论帖子
     * @param invitationRemarkDTO
     */
    public void InsertInvitationRemark(InvitationRemarkDTO invitationRemarkDTO) {
        InvitationRemark invitationRemark = new InvitationRemark();
        BeanUtils.copyProperties(invitationRemarkDTO, invitationRemark);
        //添加评论者信息
        invitationRemark.setCreateUser(Math.toIntExact(BaseContext.getCurrentId()));
        //创建时间
        invitationRemark.setCreateTime(LocalDateTime.now());

        invitationMapper.InsertInvitationRemark(invitationRemark);
    }

    /**
     * 查询是否点赞
     * @param id
     * @return
     */
    public Boolean invitationRemarkUserLikeStatus(Integer id) {
        InvitationLike invitationLike =
                invitationMapper.invitationRemarkUserLikeStatus(id ,BaseContext.getCurrentId());
        return invitationLike != null;
    }

    /**
     * 查询是否点赞
     * @param id
     * @return
     */
    public Boolean invitationRemarkUserCollectStatus(Integer id) {
        InvitationLike invitationCollect =
                invitationMapper.invitationRemarkUserCollectStatus(id ,BaseContext.getCurrentId());
        return invitationCollect != null;
    }

    /**
     * 查询帖子评论
     * @param invitationId
     * @return
     */
    public List<InvitationRemarkVO> selectInvitationRemark(Integer invitationId) {
        List<InvitationRemark> invitationRemarkList = invitationMapper.selectInvitationRemark(invitationId);
        List<InvitationRemarkVO> invitationRemarkVOList = new ArrayList<>();

        for (InvitationRemark invitationRemark : invitationRemarkList) {
            InvitationRemarkVO invitationRemarkVO = new InvitationRemarkVO();
            BeanUtils.copyProperties(invitationRemark, invitationRemarkVO);
            invitationRemarkVO.setCreateUser(userInformationMapper.selectName(invitationRemark.getCreateUser()));
            invitationRemarkVO.setProfilePhoto(userInformationMapper.selectProfilePhotoById(invitationRemark.getCreateUser()));

            invitationRemarkVOList.add(invitationRemarkVO);
        }
        return invitationRemarkVOList;
    }

    @Override
    public List<InvitationCollectVO> selectInvitationLikeById() {
        //查询喜欢表
        List<InvitationCollect> invitationCollects =
                invitationMapper.selectInvitationLikesById(BaseContext.getCurrentId());
        //查询帖子信息
        List<InvitationCollectVO> vos = new ArrayList<>();
        for (InvitationCollect invitationCollect : invitationCollects) {
            //收藏列表VO
            InvitationCollectVO vo = new InvitationCollectVO();
            //帖子详细详细E
            InvitationInformation invitationInformation =
                    invitationMapper.selectInvitationInfoById(invitationCollect.getInvitationId());
            //赋值
            BeanUtils.copyProperties(invitationInformation,vo);
            //修改
            vo.setInvitationType(invitationInfoUtil.typeToStr(invitationInformation.getInvitationType()));
            vo.setInvitationId(invitationInformation.getId());
            vo.setId(invitationCollect.getId());
            vo.setCreateUser(userInformationMapper.selectName(invitationInformation.getCreateUser()));

            vos.add(vo);
        }

        return vos;
    }

    /**
     * 查询热门帖子
     * @return
     */
    public List<InvitationHotVO> selectInvitationHot() {
        return invitationMapper.selectInvitationHot();
    }

    /**
     * 查询发表的评论
     * @return
     */
    public List<InvitationRemarkVO> selectInvitationRemarksSelf() {
        List<InvitationRemarkVO>  invitationRemarkVOList = new ArrayList<>();
        //查询列表
        List<InvitationRemark> invitationRemarkList = invitationMapper.selectInvitationRemarksSel(BaseContext.getCurrentId());
        //对实体列表进行修改
        for (InvitationRemark invitationRemark : invitationRemarkList) {
            //声明
            InvitationRemarkVO invitationRemarkVO = new InvitationRemarkVO();
            //赋值
            BeanUtils.copyProperties(invitationRemark, invitationRemarkVO);
            //修改userName
            invitationRemarkVO.setCreateUser(userInformationMapper.selectName(invitationRemark.getCreateUser()));
            invitationRemarkVOList.add(invitationRemarkVO);
        }
        return invitationRemarkVOList;
    }

    /**
     * 修改发表的评论
     * @param dto
     */
    public void updateInvitationRemarksSelf(InvitationRemarkDTO dto) {
        //声明
        InvitationRemark e = new InvitationRemark();
        BeanUtils.copyProperties(dto, e);
        //修改参数
        e.setCreateTime(LocalDateTime.now());
        e.setCreateUser(Math.toIntExact(BaseContext.getCurrentId()));
        //修改
        invitationMapper.updateInvitationRemarksSelf(e);
    }

    /**
     * 删除发表的评论
     * @param id
     */
    public void deleteInvitationRemarksSelf(Integer id) {
        invitationMapper.deleteInvitationRemarksSelf(id);
    }
}
